<?php 

namespace app\admin\controller;

use app\admin\model\Basicdata;
use app\admin\model\Member;
use app\admin\model\Povertymember;
use app\admin\model\Organization;
use app\admin\model\Farmbaseland;
use app\admin\model\Farmplanting;
use app\admin\model\Manages;
use app\admin\model\Villages;
use app\admin\model\Area;

use think\Db;
use think\Request;

class Iframe extends Base
{

    /**
     * 获取所有人员
     */
    public function memberList() {
        $request = $this->request;
        if($request->isAjax()){
            $param = $request->param();

            $start = $param['start'];  //每页起始条数
            $length = $param['length'];  //每页条数

            $where = [
                'isDelete' => 2,
            ];
            if(isset($param['townId']) && $param['townId']) {
                $where['townId'] = $param['townId'];
            }
            if(isset($param['villageId']) && $param['villageId']) {
                $where['villageId'] = $param['villageId'];
            }
            if(isset($param['memberId']) && $param['memberId']) {
                $where['id'] = ['neq', $param['memberId']];
            }

            // 搜索
            if($param['search']['value']) {
                $filter = json_decode($param['search']['value'],true);

                if(isset($filter['cityId']) && $filter['cityId']){
                    $where['cityId'] = $filter['cityId'];
                }
                if(isset($filter['xianId']) && $filter['xianId']){
                    $where['xianId'] = $filter['xianId'];
                }
                if(isset($filter['townId']) && $filter['townId']){
                    $where['townId'] = $filter['townId'];
                }
                if(isset($filter['villageId']) && $filter['villageId']){
                    $where['villageId'] = $filter['villageId'];
                }
                if(isset($filter['name']) && $filter['name']){
                    $where['name'] = ['like', '%'.$filter['name'].'%'];
                }
            }

            $field = 'id, name, mobile, memberType, cityId, xianId, townId, villageId';
            $list = Member::where($where)->field($field)->limit($start, $length)->select();
            $count = Member::where($where)->count();

            if ($list) {
                foreach ($list as $item) {
                    if ($item['memberType'] == 2) {
                        $item['village'] = '外来人口';
                    } elseif ($item['memberType'] == 3) {
                        $item['village'] = '流动人口';
                    } else {
                        if ($item['villageId']) {
                            $item['village'] = Villages::where('id', $item['villageId'])->value('name');
                        } else {
                            $item['village'] = Area::where('id', $item['townId'])->value('name');
                        }
                    }
                }
            }

            $result = [
                'status' => '1',
                'draw' => $param['draw'],
                'data' => $list,
                'recordsFiltered' => $count,
                'recordsTotal' => $count
            ];

            return json($result);
        }

        return $this->view->fetch('iframe/member_list', [
            'pageSize' => ['url' => fullUrl($request->path())],
            'param' => $request->param()
        ]);
    }

    /**
     * 获取所有贫困户
     */
    public function povertymemberList() {
        $request = $this->request;
        if($request->isAjax()){
            $param = $request->param();

            $start = $param['start'];  //每页起始条数
            $length = $param['length'];  //每页条数

            $where = [
                'b.isDelete' => 2,
            ];

            // 搜索
            if($param['search']['value']) {
                $filter = json_decode($param['search']['value'],true);

                if(isset($filter['cityId']) && $filter['cityId']){
                    $where['b.cityId'] = $filter['cityId'];
                }
                if(isset($filter['xianId']) && $filter['xianId']){
                    $where['b.xianId'] = $filter['xianId'];
                }
                if(isset($filter['townId']) && $filter['townId']){
                    $where['b.townId'] = $filter['townId'];
                }
                if(isset($filter['villageId']) && $filter['villageId']){
                    $where['b.villageId'] = $filter['villageId'];
                }
                if(isset($filter['name']) && $filter['name']){
                    $where['b.name'] = ['like', '%'.$filter['name'].'%'];
                }
            }

            $join = [
                ['__MEMBER__ b', 'b.id = a.memberId'],
            ];
            $field = 'a.povertydegree, b.id, b.name, b.mobile, b.cityId, b.xianId, b.townId, b.villageId';
            $list = Povertymember::alias('a')->where($where)->join($join)->field($field)->limit($start, $length)->select();
            $count = Povertymember::alias('a')->where($where)->join($join)->count();

            if ($list) {
                foreach ($list as $item) {
                    // 所在地
                    $item['area'] = getArea($item);
                }
            }

            $result = [
                'status' => '1',
                'draw' => $param['draw'],
                'data' => $list,
                'recordsFiltered' => $count,
                'recordsTotal' => $count
            ];

            return json($result);
        }

        return $this->view->fetch('iframe/povertymember_list', [
            'pageSize' => ['url' => fullUrl($request->path())],
            'param' => $request->param()
        ]);
    }

    /**
     * 获取所有组织
     */
    public function organizationList() {
        $request = $this->request;
        if($request->isAjax()){
            $param = $request->param();

            $start = $param['start'];  //每页起始条数
            $length = $param['length'];  //每页条数

            $where = [
                'isDelete' => 2,
            ];

            // 搜索
            if($param['search']['value']) {
                $filter = json_decode($param['search']['value'],true);

                if(isset($filter['cityId']) && $filter['cityId']){
                    $where['cityId'] = $filter['cityId'];
                }
                if(isset($filter['xianId']) && $filter['xianId']){
                    $where['xianId'] = $filter['xianId'];
                }
                if(isset($filter['townId']) && $filter['townId']){
                    $where['townId'] = $filter['townId'];
                }
                if(isset($filter['villageId']) && $filter['villageId']){
                    $where['villageId'] = $filter['villageId'];
                }
                if(isset($filter['name']) && $filter['name']){
                    $where['name'] = ['like', '%'.$filter['name'].'%'];
                }
            }

            $field = 'id, name, cityId, xianId, townId, villageId';
            $list = Organization::where($where)->field($field)->limit($start, $length)->select();
            $count = Organization::where($where)->count();

            $result = [
                'status' => '1',
                'draw' => $param['draw'],
                'data' => $list,
                'recordsFiltered' => $count,
                'recordsTotal' => $count
            ];

            return json($result);
        }

        return $this->view->fetch('iframe/organization_list', [
            'pageSize' => ['url' => fullUrl($request->path())],
            'param' => $request->param()
        ]);
    }

    /**
     * 获取种养计划
     */
    public function farmplantingList() {
        $request = $this->request;
        if($request->isAjax()){
            $param = $request->param();

            $start = $param['start'];  //每页起始条数
            $length = $param['length'];  //每页条数

            $where = [

            ];

            // 搜索
            if($param['search']['value']) {
                $filter = json_decode($param['search']['value'],true);

                if(isset($filter['croptypeId']) && $filter['croptypeId']){
                    $where['croptypeId'] = $filter['croptypeId'];
                }
                if(isset($filter['cropId']) && $filter['cropId']){
                    $where['cropId'] = $filter['cropId'];
                }
                if(isset($filter['varietieId']) && $filter['varietieId']){
                    $where['varietieId'] = $filter['varietieId'];
                }
            }

            $field = 'id, croptypeId, cropId, varietieId, expectedYield';
            $list = Farmplanting::where($where)->field($field)->limit($start, $length)->select();
            $count = Farmplanting::where($where)->count();

            if ($list) {
                foreach ($list as $item) {
                    // 类目
                    $item['croptypeName'] = Basicdata::where(['id' => $item['croptypeId']])->value('name');
                    // 作物
                    $item['cropName'] = Basicdata::where(['id' => $item['cropId']])->value('name');
                    // 品种
                    $item['varietieName'] = Basicdata::where(['id' => $item['varietieId']])->value('name');
                }
            }

            $result = [
                'status' => '1',
                'draw' => $param['draw'],
                'data' => $list,
                'recordsFiltered' => $count,
                'recordsTotal' => $count
            ];

            return json($result);
        }

        return $this->view->fetch('iframe/farmplanting_list', [
            'pageSize' => ['url' => fullUrl($request->path())],
            'param' => $request->param()
        ]);
    }

    /**
     * 获取所有生产基地
     */
    public function farmbaselandList() {
        $request = $this->request;
        if($request->isAjax()){
            $param = $request->param();

            $start = $param['start'];  //每页起始条数
            $length = $param['length'];  //每页条数

            $where = [

            ];
            if(isset($param['townId']) && $param['townId']) {
                $where['townId'] = $param['townId'];
            }
            if(isset($param['villageId']) && $param['villageId']) {
                $where['villageId'] = $param['villageId'];
            }

            // 搜索
            if($param['search']['value']) {
                $filter = json_decode($param['search']['value'],true);

                if(isset($filter['cityId']) && $filter['cityId']){
                    $where['cityId'] = $filter['cityId'];
                }
                if(isset($filter['xianId']) && $filter['xianId']){
                    $where['xianId'] = $filter['xianId'];
                }
                if(isset($filter['townId']) && $filter['townId']){
                    $where['townId'] = $filter['townId'];
                }
                if(isset($filter['villageId']) && $filter['villageId']){
                    $where['villageId'] = $filter['villageId'];
                }
                if(isset($filter['name']) && $filter['name']){
                    $where['name'] = ['like', '%'.$filter['name'].'%'];
                }
            }

            $field = 'id, imgUrl, name, cityId, xianId, townId, villageId';
            $list = Farmbaseland::where($where)->field($field)->limit($start, $length)->select();
            $count = Farmbaseland::where($where)->count();

            $result = [
                'status' => '1',
                'draw' => $param['draw'],
                'data' => $list,
                'recordsFiltered' => $count,
                'recordsTotal' => $count
            ];

            return json($result);
        }

        return $this->view->fetch('iframe/farmbaseland_list', [
            'pageSize' => ['url' => fullUrl($request->path())],
            'param' => $request->param()
        ]);
    }

    /**
     * 获取经营主体
     */
    public function manageList() {
        $request = $this->request;
        if($request->isAjax()){
            $param = $request->param();

            $start = $param['start'];  //每页起始条数
            $length = $param['length'];  //每页条数

            $where = [

            ];
            if(isset($param['townId']) && $param['townId']) {
                $where['townId'] = $param['townId'];
            }
            if(isset($param['villageId']) && $param['villageId']) {
                $where['villageId'] = $param['villageId'];
            }

            // 搜索
            if($param['search']['value']) {
                $filter = json_decode($param['search']['value'],true);

                if(isset($filter['cityId']) && $filter['cityId']){
                    $where['cityId'] = $filter['cityId'];
                }
                if(isset($filter['xianId']) && $filter['xianId']){
                    $where['xianId'] = $filter['xianId'];
                }
                if(isset($filter['townId']) && $filter['townId']){
                    $where['townId'] = $filter['townId'];
                }
                if(isset($filter['villageId']) && $filter['villageId']){
                    $where['villageId'] = $filter['villageId'];
                }
                if(isset($filter['typeId']) && $filter['typeId']){
                    $where['typeId'] = $filter['typeId'];
                }
                if(isset($filter['name']) && $filter['name']){
                    $where['name'] = ['like', '%'.$filter['name'].'%'];
                }
            }

            $field = 'id, name, typeId, cityId, xianId, townId, villageId';
            $list = Manages::where($where)->field($field)->limit($start, $length)->select();
            $count = Manages::where($where)->count();

            if ($list) {
                foreach ($list as $item) {
                    // 主体类型
                    if($item['typeId']){
                        $typeArr = get_managetype();
                        $item['typeId'] = $typeArr[$item['typeId']];
                    }
                }
            }

            $result = [
                'status' => '1',
                'draw' => $param['draw'],
                'data' => $list,
                'recordsFiltered' => $count,
                'recordsTotal' => $count
            ];

            return json($result);
        }

        // 主体类型
        $typeList = get_managetype();
        $this->assign('typeList', $typeList);

        return $this->view->fetch('iframe/manage_list', [
            'pageSize' => ['url' => fullUrl($request->path())],
            'param' => $request->param()
        ]);
    }
}